from data_define import Record
import json


class FileReader:
    def read_data(self) -> list[Record]:
        pass


class TextFileReader(FileReader):
    def __init__(self, path):
        self.path = path

    def read_data(self) -> list[Record]:
        read_list: list[Record] = []
        f = open(self.path, 'r', encoding='UTF-8')
        for line in f.readlines():
            line = line.strip()
            date_list = line.split(',')
            record = Record(date_list[0], date_list[1], date_list[2], date_list[3], )
            read_list.append(record)
        f.close()
        return read_list


class JsonFileReader(FileReader):
    def __init__(self, path):
        self.path = path

    def read_data(self) -> list[Record]:
        read_list: list[Record] = []
        f = open(self.path, 'r', encoding='UTF-8')
        for line in f.readlines():
            date_list = json.loads(line)
            record = Record(date_list['date'], date_list['order_id'], date_list['money'], date_list['province'], )
            read_list.append(record)
        f.close()
        return read_list


# 当.py文件被直接运行的时候，下方的代码将被执行；当.py文件以模块形式被导入时，if __name__ =='__main__'下代码不被执行
if __name__ == '__main__':
    text_file_reader = TextFileReader('data/2011年1月销售数据.txt')
    json_file_reader = JsonFileReader('data/2011年2月销售数据JSON.txt')
    list1 = text_file_reader.read_data()
    list2 = json_file_reader.read_data()

    for l in list1:
        print(l)

    for l in list2:
        print(l)
